#pragma warning(disable : 4819)
#include "MakeMapScene.h"
#include "Game.h"
#include "TextureLoader.h"
#include <fstream>

MakeMapScene::MakeMapScene()
{
}

MakeMapScene::~MakeMapScene()
{
    SDL_DestroyTexture(playerTexture_);
}


void MakeMapScene::Init()
{
    currentPos_ = {0,0};
    
    //初始化创建所有枚举地图块
    terrainObjs_[Empty].type        = TerrainType::Empty;
    terrainObjs_[ICE].type          = TerrainType::ICE;
    terrainObjs_[Junjle].type       = TerrainType::Junjle;
    terrainObjs_[Water].type        = TerrainType::Water;
    terrainObjs_[Iron].type         = 
    terrainObjs_[Iron_Top].type     = 
    terrainObjs_[Iron_Left].type    = 
    terrainObjs_[Iron_Bottom].type  = 
    terrainObjs_[Iron_Right].type   = TerrainType::Iron;
    terrainObjs_[Brick].type        = 
    terrainObjs_[Brick_Top].type    = 
    terrainObjs_[Brick_Left].type   = 
    terrainObjs_[Brick_Bottom].type = 
    terrainObjs_[Brick_Right].type  = TerrainType::Brick;

    terrainObjs_[Iron_Top].block_bit  = terrainObjs_[Brick_Top].block_bit  = 0xFF00;
    terrainObjs_[Iron_Left].block_bit = terrainObjs_[Brick_Left].block_bit  = 0xCCCC;
    terrainObjs_[Iron_Bottom].block_bit = terrainObjs_[Brick_Bottom].block_bit = 0x00FF;
    terrainObjs_[Iron_Right].block_bit  = terrainObjs_[Brick_Right].block_bit = 0x3333;

    playerTexture_ = IMG_LoadTexture(game_.GetRenderer(), "fodder/pic/tank_sprite.png");
}

void MakeMapScene::HandleEvent(SDL_Event *event)
{
    if(event->type == SDL_KEYDOWN)
    {
        switch (event->key.keysym.sym)
        {
        case SDLK_w:
        {
            if (currentPos_.y > 0)
            {
                --currentPos_.y;
            }
            isSwitch_ = false;
        }
        break;
        case SDLK_a:
        {
            if (currentPos_.x > 0)
            {
                --currentPos_.x;
            }
            isSwitch_ = false;
        }
        break;
        case SDLK_s:
        {
            if (currentPos_.y < (MAP_HEIGHT - 1))
            {
                ++currentPos_.y;
            }
            isSwitch_ = false;
        }
        break;
        case SDLK_d:
        {
            if (currentPos_.x < (MAP_WIDTH - 1))
            {
                ++currentPos_.x;
            }
            isSwitch_ = false;
        }
        break;
        case SDLK_j:
        {
            if(!isSwitch_)
            {
                isSwitch_ = true;
            }
            else
            {
                int type = static_cast<int>(currentIndex_);
                ++type;
                if (type >= static_cast<int>(TerrainIndexMax))
                {
                    type = 0;
                }
                currentIndex_ = static_cast<TerrainIndex>(type);
            }
            terrains_[currentPos_.x][currentPos_.y] = terrainObjs_[currentIndex_];
        }
        break;
        case SDLK_k:
        {
            if(!isSwitch_)
            {
                isSwitch_ = true;
            }
            else
            {
                int type = static_cast<int>(currentIndex_);
                --type;
                if (type < 0)
                {
                    type = static_cast<int>(TerrainIndexMax) - 1;
                }
                currentIndex_ = static_cast<TerrainIndex>(type);
            }
            terrains_[currentPos_.x][currentPos_.y] = terrainObjs_[currentIndex_];
        }
        break;
        case SDLK_RETURN://Enter 键保存地图并进入游戏
        {
            updateResult_.isSwitchScene = true;
            updateResult_.sceneType = static_cast<int>(SceneType::SceneGame);
            updateResult_.data = terrains_;
        }
        break;
        //esc键退出
        case SDLK_ESCAPE:
        {
            updateResult_.isSwitchScene = true;
            updateResult_.sceneType = static_cast<int>(SceneType::SceneBegin);
        }
        break;
        default:
            break;
        }
    }
}

UpDateResult MakeMapScene::Update(float deltaTime)
{
    static float delay = 0;
    delay += deltaTime;
    if(delay > 500)
    {
        isDraw_ = !isDraw_;
        delay = 0;
    }
    return updateResult_;
}

void MakeMapScene::Render()
{
    auto renderer = game_.GetRenderer();
    SDL_RenderClear(renderer);
    renderTerrain();
    if(isDraw_)
    {
        renderPlayer();
    }
    SDL_RenderPresent(renderer);
}

void MakeMapScene::renderTerrain()
{
    auto renderer = game_.GetRenderer();
    for (int x = 0; x < MAP_WIDTH; x++)
    {
        for(int y=0;y<MAP_HEIGHT;y++)
        {
            auto& terrain = terrains_[x][y];
            if(terrain.type == TerrainType::Empty)
            {
                continue;
            }
            auto texture = TextureLoader::Instance().GetTerrainTexture(terrain.type);
            if(terrain.type == TerrainType::Brick || terrain.type == TerrainType::Iron)
            {
                //砖块和铁块需要根据block_bit渲染
                uint16_t bit = 0x8000;
                for(int i=0;i<16;i++)
                {
                    if(terrain.block_bit & bit)
                    {
                        int offset_x = (i%4)*MAP_BLOCK_PIECE_SIZE;
                        int offset_y = (i/4)*MAP_BLOCK_PIECE_SIZE;
                        SDL_Rect srcRect = {offset_x,offset_y,MAP_BLOCK_PIECE_SIZE,MAP_BLOCK_PIECE_SIZE};
                        SDL_Rect dstRect = {x*MAP_BLOCK_WIDTH+offset_x,y*MAP_BLOCK_HEIGHT+offset_y,MAP_BLOCK_PIECE_SIZE,MAP_BLOCK_PIECE_SIZE};
                        SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
                    }
                    bit >>= 1;
                }
            }
            else
            {
                SDL_Rect dstRect = {x * MAP_BLOCK_WIDTH, y * MAP_BLOCK_HEIGHT, MAP_BLOCK_WIDTH, MAP_BLOCK_HEIGHT};
                SDL_RenderCopy(renderer, texture, nullptr, &dstRect);
            }
        }
    }
}

void MakeMapScene::renderPlayer()
{
    SDL_Rect srcRect = {0,0,34,34};
    SDL_Rect dstRect = {currentPos_.x*MAP_BLOCK_WIDTH,currentPos_.y*MAP_BLOCK_WIDTH,MAP_BLOCK_WIDTH,MAP_BLOCK_WIDTH};
    SDL_RenderCopy(game_.GetRenderer(), playerTexture_, &srcRect, &dstRect);
}

